package com.shopping.mall.core.module.saleData.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.order.entity.Delivery;
import com.shopping.mall.core.module.saleData.entity.SaleData;
import com.shopping.mall.core.module.saleData.entity.SaleDataQueryVO;
import com.shopping.mall.core.module.saleData.service.SaleDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Api(tags = "SaleData")
@RestController
@RequestMapping("/saleData")
public class SaleDataController {
    @Autowired
    private SaleDataService saleDataService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<SaleData>> getSaleDataPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        SaleDataQueryVO saleDataQueryVO) {
        IPage<SaleData> saleDataPage=saleDataService.getSaleDataPage(current,size,saleDataQueryVO);
        return Result.OK(saleDataPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<SaleData> update(@Validated(SaleData.update.class) @RequestBody SaleData saleData) {
        saleDataService.updateById(saleData);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(SaleData.insert.class) @RequestBody SaleData saleData) {
        saleDataService.save(saleData);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<SaleData> getById(@RequestParam String id){
        SaleData saleData=saleDataService.getById(id);
        return Result.OK(saleData);
    }

    @UnLogin
    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<SaleData>> getAll(SaleDataQueryVO saleDataQueryVO){
        List<SaleData> saleDataList=saleDataService.getAll(saleDataQueryVO);
        return Result.OK(saleDataList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return saleDataService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return saleDataService.batchDelete(ids);
    }

    @UnLogin
    @ApiOperation("导出数据")
    @GetMapping("/data/excel")
    public void getExcel(SaleDataQueryVO saleDataQueryVO,HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("销售量与访问量", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SaleData> data=saleDataService.getAll(saleDataQueryVO);
        EasyExcel.write(response.getOutputStream(), SaleData.class).sheet("sheet1").doWrite(data);
    }
}
